package com.main.tripplanner.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.main.tripplanner.persistent.Country;


@Repository
public class CountryDAO extends AbstractDAO{
	
	public List<Country> getAllCountries() {
		Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Country.class);
		criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
		List<Country> result = criteria.list();
		return result;
	}

	public Country getCountryById(String countryId) {
		Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Country.class);
		criteria.add(Restrictions.eq("id", Integer.parseInt(countryId)));
		List<Country> result = criteria.list();
		if(result.size() ==0){
			return null;
		}
		else{
			return result.get(0);
		}
	}

}
